from django.db import models

from BYD_ERP.utils.base_model import BaseModel2


# python ../../manage.py startapp purchase_info

# 迁移基础：erp_system, basic_info, goods_info

# python ../../manage.py makemigrations purchase_info
# python ../../manage.py migrate purchase_info

# python ../../manage.py check

# 采购单（采购订单）模型类
class PurchaseOrderModel(BaseModel2):
    invoices_date = models.DateTimeField('单据日期')
    number_code = models.CharField('单据编号，默认自动生成，不让用户填写', max_length=28)
    discount = models.DecimalField('优惠率，最多精确到小数点后两位', max_digits=5, decimal_places=2, blank=True, null=True)
    discount_money = models.DecimalField('优惠金额（采购优惠），最多精确到小数点后两位', max_digits=10, decimal_places=2, blank=True,
                                         null=True)
    remark = models.CharField('备注', max_length=512, blank=True, null=True)
    last_amount = models.DecimalField('优惠后总金额，最多精确到小数点后两位', max_digits=13, decimal_places=2, blank=True, null=True)
    deposit = models.DecimalField('支付定金，最多精确到小数点后两位', max_digits=10, decimal_places=2, blank=True, null=True)
    number_count = models.DecimalField('采购数量，最多精确到小数点后两位', max_digits=10, decimal_places=2)
    status = models.CharField('采购订单的状态/采购单的状态，0：未审核，1：已审核，2：部分入库，3：全部入库，4：已付定金，5：完成采购（货全部入库，付全款完成）',
                              max_length=10)

    operator_user = models.ForeignKey('erp_system.UserModel', related_name='operator_purchase_order_list', blank=True, null=True,
                                      on_delete=models.SET_NULL, verbose_name='制单人（采购操作人员），不能修改')

    # 增加一个冗余字段
    operator_user_name = models.CharField('制单人真实姓名', max_length=20)
    check_user = models.ForeignKey('erp_system.UserModel', blank=True, null=True, on_delete=models.SET_NULL,
                                   verbose_name='审核人员，不能修改')

    # 增加一个冗余字段
    check_user_name = models.CharField('审核人员真实姓名', max_length=20)

    account = models.ForeignKey('basic_info.SettlementAccountModel', blank=True, null=True, on_delete=models.SET_NULL,
                                verbose_name='结算账户，审核之后不能改')

    supplier = models.ForeignKey('basic_info.SupplierModel', blank=True, null=True, on_delete=models.SET_NULL,
                                 verbose_name='供应商，审核之后不能改')

    supplier_name = models.CharField('供应商名称', max_length=30, blank=True, null=True)
    attachment_list = models.CharField('附件的ID列表，字段的值为：1,2,3,4', max_length=20, blank=True, null=True)

    class Meta:
        db_table = 't_purchase_order'
        verbose_name = '采购订单表'
        verbose_name_plural = verbose_name
        app_label = 'purchase_info'
        ordering = ['id']


# 采购订单项目模型类
class PurchaseOrderItemModel(BaseModel2):

    # 这几个也是冗余字段（本质就是以存储空间换取时间）
    name = models.CharField('货品名称', max_length=20)
    specification = models.CharField('规格/件号', max_length=50, blank=True, null=True)
    model_number = models.CharField('型号', max_length=50, blank=True, null=True)
    brand = models.CharField('品牌', max_length=50, blank=True, null=True)
    nominated_supplier = models.ForeignKey('basic_info.SupplierModel', blank=True, null=True, on_delete=models.SET_NULL,
                                           verbose_name='指定供应商，审核之后不能改')
    units_name = models.CharField('计量单位名称', max_length=50, blank=True, null=True)
    units = models.ForeignKey('goods_info.UnitsModel', on_delete=models.SET_NULL, blank=True, null=True)
    remark = models.CharField('备注', max_length=512, blank=True, null=True)
    purchase_count = models.DecimalField('采购数量，最多精确到小数点后两位', max_digits=10, decimal_places=2, blank=True, null=True)
    purchase_price = models.DecimalField('采购价格，最多精确到小数点后两位', max_digits=10, decimal_places=2, blank=True, null=True)
    purchase_money = models.DecimalField('采购金额，最多精确到小数点后两位', max_digits=10, decimal_places=2, blank=True, null=True)

    required_date = models.DateTimeField('需求日期', blank=True, null=True)

    purchase_order = models.ForeignKey('PurchaseOrderModel', related_name='item_list', blank=True, null=True,
                                       on_delete=models.CASCADE, verbose_name='采购订单')

    goods = models.ForeignKey('goods_info.GoodsModel', blank=True, null=True, on_delete=models.CASCADE,
                              verbose_name='货品')
    warehouse = models.ForeignKey('basic_info.WarehouseModel', blank=True, null=True, on_delete=models.CASCADE,
                                  verbose_name='仓库')

    class Meta:
        db_table = 't_purchase_order_items'
        verbose_name = '采购订单项目表'
        verbose_name_plural = verbose_name
        ordering = ['id']

    def __str__(self):
        return self.name + ' ' + self.specification
